/**  
 * Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
 * in source and binary forms, with or without modification, are permitted  
 * provided that the following conditions are met: Redistributions of source  
 * code must retain the above copyright notice, this list of conditions and the  
 * following disclaimer. Redistributions in binary form must reproduce the above  
 * copyright notice, this list of conditions and the following disclaimer in the  
 * documentation and/or other materials provided with the distribution. Neither  
 * the name of Noel Lynch nor the names of contributors may be used to  
 * endorse or promote products derived from this software without specific prior  
 * written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
 */
// May 1, 2009

package com.sceneworker.editors.terraincreator.heightmaps;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import com.jmex.terrain.util.AbstractHeightMap;
import com.jmex.terrain.util.ImageBasedHeightMap;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.SwingComponentFactory;

public class ImageBasedHeightMapPanel extends JPanel implements IGenerateHeightMap {

	private		BufferedImage		mcl_loadedImage;

	public	ImageBasedHeightMapPanel() {
		setLayout(new MigLayout("inset 5"));

		JButton		lcl_loadBtn = SwingComponentFactory.makeAButton("Load");
		lcl_loadBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				File		lcl_file = GenericUtils.getAFile(null, false, false);

				if(lcl_file == null) { return; }

				try {
					mcl_loadedImage = ImageIO.read(lcl_file);
					repaint();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		});

		add(lcl_loadBtn, "wrap");

		JComponent		lcl_image = new JComponent() {
			@Override
			protected void paintComponent(Graphics cl_g) {
				super.paintComponent(cl_g);

				if(mcl_loadedImage != null) {
					cl_g.drawImage(mcl_loadedImage, 0, 0, getWidth(), getHeight(), this);
				}
			}

			@Override
			public Dimension getPreferredSize() {
				return new Dimension(256, 256);
			}

		};

		add(lcl_image, "width 100%, height 100%");
	}

	public AbstractHeightMap generateHeightMap() {
		ImageBasedHeightMap		lcl_hmap = new ImageBasedHeightMap(mcl_loadedImage);

		return lcl_hmap;
	}

}
